package com.mjlf.test_shiro.controller;

import com.mjlf.test_shiro.service.LoginService;
import com.mjlf.test_shiro.service.PermissionService;
import com.mjlf.test_shiro.service.PersonService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @ClassName: LoginController
 * @Author: majun
 * @CreateDate: 2018/10/17 17:05
 * @Version: 1.0
 * @Description: TODO
 */

@RestController
@RequestMapping("/test_shiro")
@EnableSwagger2
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private PersonService personService;
    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "登录接口",notes = "登录接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "I don't know what message")
    })
    @RequestMapping(path = "/login", method = RequestMethod.GET)
    public String login(@RequestParam String username, @RequestParam String password){
        System.out.println(username + "-" + password);
        if(loginService.login(username, password)){
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
//            usernamePasswordToken.setRememberMe(true);

            Subject user = SecurityUtils.getSubject();
            user.login(usernamePasswordToken);
            return "登录成功";
        }
        return "登录失败";
    }
}
